import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: { requiresAuth: true }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
    },
    {
      path: '/profile',
      name: 'profile',
      component: () => import('../views/ProfileView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/health-consultation',
      name: 'health-consultation',
      component: () => import('../views/HealthConsultationView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/consultation-detail/:consultId',
      name: 'consultation-detail',
      component: () => import('../views/ConsultationDetailView.vue'),
      meta: { requiresAuth: true }
    },
            {
          path: '/price-detail',
          name: 'price-detail',
          component: () => import('../views/PriceDetailView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/discounts',
          name: 'discounts',
          component: () => import('../views/DiscountView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/medical-guide',
          name: 'medical-guide',
          component: () => import('../views/MedicalGuideView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/guide-detail/:id',
          name: 'guide-detail',
          component: () => import('../views/GuideDetailView.vue'),
          meta: { requiresAuth: true }
        },



    
  ],
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  
  if (to.meta.requiresAuth && !token) {
    // 需要登录但未登录，跳转到登录页
    next('/login')
  } else if (to.path === '/login' && token) {
    // 已登录用户访问登录页，跳转到首页
    next('/')
  } else {
    next()
  }
})

export default router
